import type { FormInstance, FormItemProps, FormProps } from 'antd'
import type React from 'react'

export interface FormSchemaItem<T extends object = any, K extends keyof T & string = any> {
  /** 字段名 */
  name: K
  span?: number | ((props: { record: T; name: K }) => number)
  /** 标签 */
  label?: string | ((props: { record: T; name: K }) => React.ReactNode)
  // 支持组件类型或已构建的 JSX 元素
  component?: React.ComponentType<any> | React.ReactElement
  // 透传给组件的 props
  componentProps?: Record<string, any> | ((values: T) => Record<string, any>)
  /** 透传给 FormItem 的 props */
  formItemProps?: FormItemProps | ((props: { record: T }) => FormItemProps)
  // 查看态的自定义渲染；若未提供则用初始值文案
  renderView?: (props: { record: T; name: K }) => React.ReactNode
  editable?: boolean | ((record: T) => boolean)
  hidden?: boolean | ((record: T) => boolean)
  /** 当这些字段变化时，当前项需要重新计算（hidden/label/componentProps等） */
  dependencies?: (keyof T & string)[]
}

type TypeFormProps<T extends object = any> = Omit<FormProps<T>, 'form'>

export interface EditFormProps<T extends object = any> {
  formProps?: TypeFormProps<T>
  formSchema: FormSchemaItem<T, keyof T & string>[]
  editable?: boolean
  form?: FormInstance<T>
  span?: number
}
